<?php
namespace app\shop\model;

use app\BaseModel;
use app\farm\model\ShopGoods;
use think\facade\Db;

class SeckillGoods extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_v2_seckill_goods';



    protected $append = [

        'show_data',

        'goods_info',

        'all_stock',

        'all_have_stock',

    ];



    /**
     * @author chenniang
     * @DataTime: 2021-10-29 18:50
     * @功能说明:总的库存
     */
    public function getAllStockAttr($value,$data){

        if(!empty($data['id'])){

            $i_model = new SeckillConfig();

            $num = $i_model->where(['atv_id'=>$data['id']])->sum('stock');

            return $num;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 18:50
     * @功能说明:总的库存
     */
    public function getAllHaveStockAttr($value,$data){

        if(!empty($data['id'])){

            $i_model = new SeckillConfig();

            $num = $i_model->where(['atv_id'=>$data['id']])->sum('have_stock');

            return $num;
        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-21 18:29
     */
    public function getGoodsInfoAttr($value,$data){

        if(!empty($data['id'])){

            $config_model = new SeckillConfig();

            $list = $config_model->alias('a')
                ->join('lbfarm_v2_shop_spe_price b','a.spe_id = b.id')
                ->where(['a.atv_id' => $data['id']])
                ->field(['b.stock as goods_stock','b.price as goods_price','a.*'])
                ->select()
                ->toArray();
            return $list;

        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-21 14:19
     */
    public function getShowDataAttr($value,$data){

        if(!empty($data['id'])){

            $config_model = new SeckillConfig();

            $spe_model = new GoodsSpePrice();

            $info = $config_model->where(['atv_id'=>$data['id']])->order('price')->find();

            if(!empty($info)){

                $info = $info->toArray();

                $info['init_price'] = $spe_model->where(['id'=>$info['spe_id']])->value('price');

            }

            return $info;

        }

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @param $dis
     * @param int $page
     * @功能说明:商品列表
     * @author chenniang
     * @DataTime: 2022-07-25 14:59
     */
    public function goodsList($dis,$page=10){

        $data = $this->alias('a')
                ->join('lbfarm_shop_goods b','a.goods_id = b.id')
                ->join('lbfarm_v2_goods_store c','c.goods_id = b.id AND c.type=1')
                ->join('lbfarm_farmer d','c.store_id = d.id')
                ->join('lbfarm_v2_seckill_list e','a.atv_id = e.id')
                ->where($dis)
                ->field('a.*,b.goods_name,b.cover,d.title as store_name,d.id as store_id,d.cover as store_cover')
                ->group('a.id')
                ->order('b.top desc,a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;


    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-25 17:19
     * @功能说明:增加销量
     */
    public function delAtvStock($atv_id,$goods_id,$spe_id,$num){

        $config_model = new SeckillConfig();

        $dis = [

            'a.atv_id'  => $atv_id,

            'a.goods_id'=> $goods_id,

            'b.spe_id'  => $spe_id
        ];

        $data = $this->alias('a')
                ->join('lbfarm_v2_seckill_config b','a.id = b.atv_id')
                ->where($dis)
                ->field('b.*')
                ->find();

        if(empty($data)){

            return ['code'=>500,'msg'=>'该活动已经下架'];
        }

        $data = $data->toArray();

        if($data['have_stock']+$num>$data['stock']){

            return ['code'=>500,'msg'=>'可兑换数量不足'];

        }

        $res = $config_model->where(['id'=>$data['id']])->update(['have_stock'=> Db::Raw("have_stock+$num")]);

        return $res;
    }




    /**
     * @author chenniang
     * @DataTime: 2022-07-25 17:19
     * @功能说明:减少销量
     */
    public function incAtvStock($atv_id,$goods_id,$spe_id,$num){

        $config_model = new SeckillConfig();

        $dis = [

            'a.atv_id'  => $atv_id,

            'a.goods_id'=> $goods_id,

            'b.spe_id'  => $spe_id
        ];

        $data = $this->alias('a')
            ->join('lbfarm_v2_seckill_config b','a.id = b.atv_id')
            ->where($dis)
            ->field('b.*')
            ->find();

        if(empty($data)){

            return true;
        }

        $data = $data->toArray();

        $res = $config_model->where(['id'=>$data['id']])->update(['have_stock'=> Db::Raw("have_stock-$num")]);

        return $res;
    }


    /**
     * @param $atv_id
     * @param $goods_id
     * @param $spe_id
     * @param $num
     * @param int $type
     * @功能说明:修改活动销量
     * @author chenniang
     * @DataTime: 2022-07-25 17:47
     */
    public function updateAtvStock($atv_id,$goods_id,$spe_id,$num,$type=1){

        if($type==1){

            $res = $this->delAtvStock($atv_id,$goods_id,$spe_id,$num);

        }else{

            $res = $this->incAtvStock($atv_id,$goods_id,$spe_id,$num);

        }

        return $res;
    }




}